#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "trdp_mini.h"

INT8U data[1300];
TRDP_MINI_ERR_T trdp_mini_pd_callback(void *arg, INT32U comId, INT8U *pData, INT32U dataSize, INT8U *recvIP, INT16U recvPort, TRDP_MINI_ERR_T errCode);
TRDP_MINI_ERR_T trdp_mini_md_callback(	void 	*arg,
										INT32U                  comId,
										MD_ACTION_T				action,
										INT8U                   *pData,
										INT32U                  dataSize,
										INT32U  				etbTopoCnt,
										INT8U   				*pSessionID,
										INT8S   				*pSrcURI,
										INT8S   				*pDestURI,
										INT32U  				seqCnt,
										INT8U					*recvIP,
										INT16U					recvPort,
										TRDP_MINI_ERR_T			errCode);

TRDP_MINI_ERR_T trdp_mini_pd_callback(void *arg, INT32U comId, INT8U *pData, INT32U dataSize, INT8U *recvIP, INT16U recvPort, TRDP_MINI_ERR_T errCode)
{
	TRDP_MINI_ERR_T ret = TRDP_MINI_NO_ERR;
	int i = 0;

	if(errCode >= 0){
		//errCode == 0, trdp communication is OK, else user should check the errCode to get the err information
		switch(comId){
		case 2000:
			for(i=0; i<dataSize; i++){
				printf("%2x ", pData[i]);
			}
			printf("\n");
			break;
		default:
			break;
		}
	}else{
		printf("TRDP STACK ERROR with errCode = %d\n",errCode);
	}

	return ret;
}



TRDP_MINI_ERR_T trdp_mini_md_callback(	void 	*arg,
										INT32U                  comId,
										MD_ACTION_T				action,
										INT8U                   *pData,
										INT32U                  dataSize,
										INT32U  				etbTopoCnt,
										INT8U   				*pSessionID,
										INT8S   				*pSrcURI,
										INT8S   				*pDestURI,
										INT32U  				seqCnt,
										INT8U					*recvIP,
										INT16U					recvPort,
										TRDP_MINI_ERR_T			errCode)
{
	INT8U srcIP[2][4] = {{10, 0, 8, 77}, {192, 168, 1, 251}};
	INT8U destIP[2][4] = {{239, 255, 0, 6},{239, 255, 0, 7}};
	TRDP_MINI_ERR_T ret = TRDP_MINI_NO_ERR;

	if(errCode >= 0){
		switch(comId){
		case 86:
			printf("%s\n", pData);

			trdp_mini_md_reply(87,  pData, dataSize, srcIP[0], destIP[0], etbTopoCnt,
												2000, 0, pSrcURI, pDestURI,pSessionID,
												0, seqCnt, trdp_mini_md_callback);

			break;
		}

	}else{
		printf("TRDP STACK ERROR with errCode = %d\n",errCode);
	}

	return ret;
}


int main(int argc, char** argv)
{
	int tickCnt = 0;
	TRDP_MINI_ERR_T ret;
	const char * ifAddress[] = {"10.0.8.77","192.168.1.251"};
	const char * mcAddress[] = {"239.255.0.6", "239.255.0.7"};
	const char srcURI[]="request", destURI[]="response";

	INT8U srcIP[2][4] = {{10, 0, 8, 77}, {192, 168, 1, 251}};
	INT8U destIP[2][4] = {{239, 255, 0, 6},{239, 255, 0, 7}};
	memset(data, 0, sizeof(data));

	sprintf((char *)data, "DLS\n");

	if(trdp_mini_init() == TRDP_MINI_NO_ERR)
	{
		ret = trdp_mini_pd_subscribe(2000, srcIP[0], destIP[0], COM_UDP_MULTICAST, trdp_mini_pd_callback);
		if(ret != TRDP_MINI_NO_ERR){
			printf("trdp_mini_pd_subscribe ERROR: %d\n",ret);
		}

		ret = trdp_mini_pd_prepare(3000, data, 1300, srcIP[0], destIP[0], 0, 1000, COM_UDP_MULTICAST);
		if(ret != TRDP_MINI_NO_ERR){
			printf("trdp_mini_pd_prepare ERROR: %d\n",ret);
		}

		ret = trdp_mini_pd_prepare(3000, data,1300, srcIP[1], destIP[1], 0, 1000, COM_UDP_MULTICAST);
		if(ret != TRDP_MINI_NO_ERR){
			printf("trdp_mini_pd_prepare ERROR: %d\n",ret);
		}

		ret = trdp_mini_md_addListener(86, srcIP[0], destIP[1], trdp_mini_md_callback);
		if(ret != TRDP_MINI_NO_ERR){
			printf("trdp_mini_md_addListener ERROR: %d\n",ret);
		}


		for(;;){
			tickCnt++;
			trdp_mini_pd_put(3000, data, strlen((char *)data), srcIP[0], destIP[0]);
			trdp_mini_pd_put(3000, data, strlen((char *)data), srcIP[1], destIP[1]);
			if((tickCnt % 5) == 0){
				trdp_mini_md_request(86, data, 1300, srcIP[0], destIP[0], 0, 1000, 1, (INT8S *)srcURI, (INT8S *)destURI, trdp_mini_md_callback);
			}
			usleep(1000*1000);

		}
	}

	return OK;
}

